function [label] = cluster( image,  k, methodName)
% CLUSTER    cluster the remote sensing image
% 
% Read a remote sensing image 
% Read the name of cluster algorithm and the parameter
% Each pixel with several bands is regarded as one example
% Cluster pixels according to the spectral information
% Up to now, Kmeans and FuzzyKmeans has been integrated
% label= cluster(image, k, methodName)
%
% Input:
% image: the original remote sensing image
% k: parameter of the cluster algorithm which denotes the number of
% clusters
% methodname: the name of cluster algorithm
% the default algorithm is Kmeans
%
% Output:
% label: the label matrix
% each pixel is assigned one label
% the pixels with same label are grouped into one cluster
%%%%%%%%%%%%%%%%%

% check the parameter
if ~exist('methodName','var')
    methodName='Kmeans';
end
imageSize=size(image);
numPixels=imageSize(1)*imageSize(2);
tempData=reshape(image,numPixels,imageSize(3));
% cluster
%
switch methodName
    case 'Kmeans'
       label=kmeans(tempData,k);
    case 'FCM'
        [c,U] = fcm(tempData,k);
        maxU=max(U);
        l=cell(1,k);
        label=zeros(numPixels,1);
        for i=1:k
            l{i}=find(U(i,:)==maxU);
            label(l{i})=i;
        end
    otherwise
        error('this method does not exist')
end
label=reshape(label,imageSize(1),imageSize(2));
end

